<!--
 * @Author: hucc
 * @Date: 2021-10-12 15:06:16
 * @LastEditors: hucc
 * @LastEditTime: 2021-10-12 19:10:06
 * @Description: 
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
</head>

<body>
    <div id="app">
        <div class='btns'>
            <button @click="isActive='myCom1' ">组件一</button>
            <button @click="isActive='myCom2' ">组件二</button>
        </div>
        <!-- <my-com1 v-if='isActive'></my-com1>
        <my-com2 v-else></my-com2> -->
        <!-- 使用components标签实现组件的动态渲染(加载) -->
        <!-- component标签在每次切换每次都会重新创建组件，开销很大 -->
        <!-- keep-alive标签帮助解决component问题 只有在第一次切换才会创建 以后切换不会创建 开销小 -->
        <keep-alive>
            <component :is="isActive"></component>
        </keep-alive>
    </div>
    <script>
        let component1 = {
            data() {
                return {}
            },
            template: `
              <div>
                <h3>我是组件一</h3>
                </div>
            `,
            //组件是vue实例 只要每次创建都会调用钩子函数
            //这里created()钩子函数测试 keep-alive的效果
            created() {
                console.log('创建组件一')
            }
        }
        let component2 = {
            data() {
                return {}
            },
            template: `
              <div>
                <h3>我是组件二</h3>
                </div>
            `,
            created() {
                console.log('创建组件二')
            }
        }
        let vm = new Vue({
            el: '#app',
            data: {
                isActive: ''
            },
            methods: {},
            created() {},
            components: {
                'myCom1': component1,
                'myCom2': component2
            }
        })
    </script>
</body>

</html>